package com.lzh.chatnote.webSocket.service.adapter;


import com.lzh.chatnote.chat.domain.vo.response.message.MessageResp;
import com.lzh.chatnote.user.domain.entity.User;
import com.lzh.chatnote.webSocket.domain.vo.resp.WSBaseResp;
import com.lzh.chatnote.webSocket.domain.vo.resp.ws.WSLoginSuccess;
import com.lzh.chatnote.webSocket.domain.vo.resp.ws.WSLoginUrl;
import com.lzh.chatnote.webSocket.enums.WSRespTypeEnum;
import org.springframework.stereotype.Component;


@Component
public class WSAdapter {
    //@Autowired
    //private ChatService chatService;


    /**
     * 构建用户发送消息的响应
     */
    public static WSBaseResp<MessageResp> buildUserSendMsgResp(MessageResp messageResp) {
        WSBaseResp<MessageResp> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.PUSH_NEW_MSG.getType());
        wsBaseResp.setData(messageResp);
        return wsBaseResp;
    }


    /**
     * 构建用户确认登录的响应
     */
    public static WSBaseResp<?> buildSureLoginResp() {
        WSBaseResp<WSLoginUrl> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.AUTHORIZATION.getType());
        return wsBaseResp;
    }

    /**
     * 构建用户token实效的响应，让前端清除token
     */
    public static WSBaseResp<?> buildInvalidateTokenResp() {
        WSBaseResp<WSLoginUrl> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.INVALIDATE_TOKEN.getType());
        return wsBaseResp;
    }

    /**
     * 第三方请求登录连接url 或者微信扫码登录二维码url
     *
     * @param url 网址
     * @return WSBaseResp
     */
    public static WSBaseResp<WSLoginUrl> buildLoginResp(String url) {
        WSBaseResp<WSLoginUrl> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_URL.getType());
        wsBaseResp.setData(WSLoginUrl.builder().loginUrl(url).build());
        return wsBaseResp;
    }


    /**
     * 登录成功 返回用户信息
     *
     * @param user    用户
     * @param token   token
     * @param isAdmin 管理
     * @return WSBaseResp
     */
    public static WSBaseResp<WSLoginSuccess> buildLoginSuccessMessageResp(User user, String token, Boolean isAdmin) {
        WSBaseResp<WSLoginSuccess> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_SUCCESS.getType());
        WSLoginSuccess loginInfo = WSLoginSuccess.builder().avatar(user.getAvatar()).name(user.getName()).power(isAdmin ? 1 : 0).token(token).uid(user.getId()).build();
        wsBaseResp.setData(loginInfo);
        return wsBaseResp;
    }

    /**
     * @return WSBaseResp
     */
    public static WSBaseResp<WSLoginSuccess> againLogin() {
        WSBaseResp<WSLoginSuccess> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.INVALIDATE_TOKEN.getType());
        return wsBaseResp;
    }


}
